<?php
/**
 * 《入职信息》实现类
 *
 * @author 孙晓晔
 * @version $Id: Work.class.php 1035 2010-03-26 05:24:59Z sunxy $
 */
require_once(dirname(__FILE__) . '/EmpUtil.class.php');

class EmpJob extends EmpUtil {
	public $tbl = Constant::tbl_emp_job;
	
	public $orderby = array(
		'-' => '请选择',
		'name' => '名称',

		't1' => '合同期起',
		't2' => '合同期止',
		'day' => '合同期限',
	
		'1' => '创建时间',
		'2' => '更新时间',
	);

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct($request);
	}

	function pretreat($controller) {
		parent::pretreat($controller);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function find($controller, $request) {
		$this->pretreat($controller);

		$controller->display($request, 'emp/job/find');
		return true;
	}

	function _orderby_x($controller, $row, $px) {
		$orderby = $this->orderby['1'];
		$row['orderby_x'] = $row['createtime'];

		if (empty($px) || $px == '-' || $px == '1') {
			$controller->assign('orderby', $orderby);

			return $row;
		}

		if ($px == '2') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['modifytime'];
		}

		if ($px == '`join`') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['join'];
		}

		$controller->assign('orderby', $orderby);

		return $row;
	}

	function _where($request) {		
		$date1_s = $request['date1_s'];
		$date2_s = $request['date2_s'];
		
		$date3_s = $request['date3_s'];
		$date4_s = $request['date4_s'];

		$my = $this->_my_add();
		if (empty($my)) {
			$where = " WHERE 1=1";
		} else {
			$where = " WHERE " . $my;
		}
		
		if (!empty($date1_s)) {
			$where .= " AND t1 >= '" . $date1_s . "'";
		}

		if (!empty($date2_s)) {
			$where .= " AND t1 < '" . $date2_s . "'";
		}
		
		if (!empty($date3_s)) {
			$where .= " AND t2 >= '" . $date3_s . "'";
		}

		if (!empty($date4_s)) {
			$where .= " AND t2 < '" . $date4_s . "'";
		}

		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);

		return $where;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function findPost($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {

			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];
				$row = $this->_view(null, $row);

				$row = $this->_orderby_x($controller, $row, $this->orderby_s);

				$data[$count] = $row;
				$count++;
			}
			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}

		$controller->display($request, 'emp/job/list');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function add($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];

		$controller->assign_by_ref('emp', $this->_emp_x($id));

		$this->_my_emp($controller, $id);

		$controller->display($request, 'emp/contract/add');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function addPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];

		$t1 = $request['t1'];
		$t2 = $request['t2'];
		$day = $request['day'];

		$name = $request['name'];
		$content = $request['content'];

		$db = newdb($this->dsn_w);

		$fmt = "INSERT INTO %s(emp, t1, t2, day, name, content, createtime, modifytime)";
		$fmt .= " VALUES('%s', '%s', '%s', '%s', '%s', '%s', now(), now())";
		$sql = sprintf($fmt, $this->tbl, $id, $t1, $t2, $day, $name, $content);
		// debug($sql);
		$db->query($sql);

		$this->add($controller, $request);
		return true;
	}

	
	
	function copy($controller, $request) {
		$this->pretreat($controller);
	
		$id = $request['id'];
		$row = $this->_view($id);
	
		$controller->assign_by_ref('result', $row);
	
		$controller->display($request, 'emp/job/copy');
		return true;
	}
	
	function copyPost($controller, $request) {
		$this->pretreat($controller);
	
		$emp = $this->_input($request['emp']);
		$office = $this->_office($emp);
		$dept = $this->_emp_dept($emp);
	
		$t1 = $request['t1'];
		$t2 = $request['t2'];
	
		$name = addslashes($request['name']);
		$content = addslashes($request['content']);
	
		$db = newdb($this->dsn_w);
	
		$fmt = "INSERT INTO %s(office, dept, emp, name, t1, t2, content, oper, createtime, modifytime)";
		$fmt .= " VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', now(), now())";
		$sql = sprintf($fmt, $this->tbl, $office, $dept, $emp, $name, $t1, $t2, $content, $_SESSION['id']);
		// debug($sql);
		$db->query($sql);
	
		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function update($controller, $request) {
		$this->pretreat($controller);
		
		$name_option = array(
			'-' => '请选择',
			'网上招聘' => '网上招聘',
			'现场招聘' => '现场招聘',
			'员工介绍' => '员工介绍',
			'员工返厂' => '员工返厂',
			'厂门招聘' => '厂门招聘',
		);
		$controller->assign_by_ref('name_option', $name_option);

		$id = $request['id'];
		
		require_once 'Emp.class.php';
		$aa = new Emp();
		$emp = $aa->_view($id);
		$controller->assign_by_ref('emp', $emp);
		
		$db = newdb($this->dsn_w);
		$fmt = "SELECT * FROM %s WHERE emp = '%s' ORDER BY id DESC";
		$sql = sprintf($fmt, $this->tbl, $id);
		debug($sql);
		$result = $db->query($sql);
		if ($row = $db->fetch_array($result)) {
			$id = $row['id'];
		} else {
			$office = $this->_office($id);
			$dept = $this->_emp_dept($id);
		
			$fmt = "INSERT INTO %s(office, dept, emp, t1, createtime, modifytime) VALUES('%s', '%s', '%s', '%s', now(), now())";
			$sql = sprintf($fmt, $this->tbl, $office, $dept, $id, $emp['createtime']);
			debug($sql);
			$db->query($sql);
			$id = $db->insert_id();
		}
		
		$row = $this->_view($id);
		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'emp/job/update');
		return true;
	}
	
	function updatePost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$emp = $this->_input($request['emp']);
		$office = $this->_office($emp);
		$dept = $this->_emp_dept($emp);
		
		$name = $request['name'];
		$t1 = $request['t1'];

		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET office = '%s', dept = '%s', t1 = '%s', name = '%s', modifytime = now() WHERE id = %s";
		$sql = sprintf($fmt, $this->tbl, $office, $dept, $t1, $name, $id);
		debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}

		// $controller->assign('baseLink', $this->doGet($request, 'findPost'));

		// $this->findPost($controller, $request);
		return true;
	}
	
	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function edit($controller, $request) {
		$this->pretreat($controller);
	
		$id = $request['id'];
		$row = $this->_view($id);
	
		$controller->assign_by_ref('result', $row);
	
		$controller->display($request, 'emp/job/edit');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function editPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$t1 = $request['t1'];
		$t2 = $request['t2'];

		$name = addslashes($request['name']);
		$content = addslashes($request['content']);

		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET t1 = '%s', t2 = '%s', name = '%s', content = '%s', modifytime = now() WHERE id = %s";
		$sql = sprintf($fmt, $this->tbl, $t1, $t2, $name, $content, $id);
		debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function view($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'emp/job/view');
		return true;
	}
	
	function _view($id = 0, $row = null) {
		if ($row == null) {
			clean($this->tbl . "_id_" . $id);
			$row = $this->id($this->tbl, $id);
		}
		
		$row['d1'] = $this->_date_d1($row['t1'], $row['t2']);
		$row['d2'] = $this->date_1(date("Y-m-d"), $row['t2']);
		
		// $row['d3'] = $this->_t3($row['t2']);
		$row = $this->_view_t($row);
		
		return $row;
	}
	
	function _date_d1($t1, $t2) {
		if (empty($t2) || $t2 == '0000-00-00') {
			$t2 = date("Y-m-d");
		}
		
		$ret = $this->date_1($t1, $t2);
		
		return $ret;
	}

	function delete($controller, $request) {
		$this->pretreat($controller);

		while (list($k, $id) = each($request)) {
			if (substr($k, 0, 2) <> "c_") {
				continue;
			}
			
			$row = $this->_view($id);
			if (!$this->_is_delete($row)) {
				continue;
			}

			$this->_delete3($id);
		}
		
		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function _is_delete($row) {
		if ($this->_is_admin()) {
			return true;
		}

		if ($row['emp'] == $_SESSION['id']) {
			return true;
		}

		return false;
	}
	
	function _delete3($id) {
		$db = newdb($this->dsn_w);
		if ($this->_is_admin()) {
			$fmt = "DELETE FROM %s WHERE id = '%s'";
		} else {
			$fmt = "UPDATE %s SET visible = 2 WHERE id = '%s'";
		}
		
		$sql = sprintf($fmt, $this->tbl, $id);
		debug($sql);
		$db->query($sql);
	}
	
	function num($id) {
		$db = newdb($this->dsn_r);
		$fmt = "SELECT COUNT(*) t FROM %s WHERE emp = '%s'";
		$sql = sprintf($fmt, $this->tbl, $id);
		// debug($sql);
		$result = $db->query($sql);
		$row = $db->fetch_array($result);

		return $row['t'];
	}
	
	function _join($emp, $name, $t1, $oper) {
		$db = newdb($this->dsn_w);
		$fmt = "INSERT INTO %s(emp, name, t1, oper, createtime, modifytime)";
		$fmt .= " VALUES('%s', '%s', '%s', '%s', now(), now())";
		$sql = sprintf($fmt, $this->tbl, $emp, $name, $t1, $oper);
		debug($sql);
		$db->query($sql);
	}
	
	function _insert($emp) {
		$db = newdb($this->dsn_w);
		$fmt = "INSERT INTO %s(emp, t1, createtime, modifytime)";
		$fmt .= " VALUES('%s', now(), now(), now())";
		$sql = sprintf($fmt, $this->tbl, $emp);
		debug($sql);
		$db->query($sql);
	}
	
	function _leave($emp, $t2, $content) {
		$db = newdb($this->dsn_w);
		$fmt = "SELECT * FROM %s WHERE emp = '%s' ORDER BY id DESC";
		$sql = sprintf($fmt, $this->tbl, $emp);
		debug($sql);
		$result = $db->query($sql);
		if ($row = $db->fetch_array($result)) {
			$id = $row['id'];
		} else {
			return;
		}
		
		$fmt = "UPDATE %s SET t2 = '%s', content = '%s', status = '3', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $t2, $content, $id);
		debug($sql);
		$db->query($sql);
	}
	
	function _lastest($emp) {
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE emp = '%s' ORDER BY id DESC";
		$sql = sprintf($fmt, $this->tbl, $emp);
		debug($sql);
		$result = $db->query($sql);
		if ($row = $db->fetch_array($result)) {
			
			// $row = $this->_view(null, $row);
			
			return $row;
		}
		
		return false;
	}
	
	function _leave_dd($emp) {
		if ($row = $this->_lastest($emp)) {
			$t2 = $row['t2'];
			$dd = date("Y-m-d");
			
			debug(print_r($row, true));
			
			return $this->date_1($t2, $dd);
		}
		
		return '';
	}
	
	function view_emp($controller, $request) {
		$this->pretreat($controller);
	
		$id = $request['id'];
		
		$ret = $this->_list_emp($id);
		
		$controller->assign_by_ref('result', $ret);
	
		$controller->display($request, 'emp/job/_list');
		return true;
	}
}
?>